package com.ayu.gulimall.seckill.schedule;


import com.ayu.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j


public class SeckillSkuSchedule {

//    每天凌晨三点上架最近三天的商品
    @Autowired
    SeckillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    private final String upload_lock ="seckill:upload:lock";

    @Async
    @Scheduled(cron = "* * 3 * * ?")
    public void addSeckillSku(){
        log.info("hello");
        log.info("上架秒杀商品中......");
        //分布式锁---->幂等性处理
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock(10, TimeUnit.SECONDS);

        try{
            seckillService.getLast3DaySkus();
        }finally {
            lock.unlock();
        }

    }
}
